<template>
  <div class="app-container">
    <!-- Search Form -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="友链id" prop="friendshipId">
        <el-input v-model="queryParams.friendshipId" placeholder="请输入友链id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="网站名" prop="friendshipName">
        <el-input v-model="queryParams.friendshipName" placeholder="请输入网站名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="邮箱" prop="friendshipEmail">
        <el-input v-model="queryParams.friendshipEmail" placeholder="请输入邮箱" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="审核状态" prop="friendshipStatus">
        <el-select v-model="queryParams.friendshipStatus" placeholder="请选择审核状态" clearable>
          <el-option v-for="dict in bk_friendship_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- Action Buttons -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['bk:friendship:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['bk:friendship:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- Card View -->
    <el-row v-loading="loading" :gutter="16">
      <el-col v-for="item in friendshipList" :key="item.friendshipId" :xs="24" :sm="12" :md="8" :lg="6" :xl="4"
        class="card-col">
        <el-card :body-style="{ padding: '0px' }" shadow="hover" class="friendship-card">
          <div class="card-header">
            <el-avatar :size="64" :src="item.friendshipImg" fit="cover" class="avatar">
              <el-icon>
                <User />
              </el-icon>
            </el-avatar>
            <div class="status-tag">
              <dict-tag :options="bk_friendship_status" :value="item.friendshipStatus" />
            </div>
          </div>
          <div class="card-content">
            <h3 class="site-name">{{ item.friendshipName }}</h3>
            <p class="site-excerpt">{{ item.friendshipExcerpt }}</p>
            <div class="site-info">
              <p><el-icon>
                  <Link />
                </el-icon> <a :href="item.friendshipUrl" target="_blank">{{ item.friendshipUrl }}</a></p>
              <p><el-icon>
                  <Message />
                </el-icon> {{ item.friendshipEmail }}</p>
            </div>
          </div>
          <div class="card-footer">
            <el-button type="primary" text icon="Edit" @click="handleUpdate(item)"
              v-hasPermi="['bk:friendship:edit']">修改</el-button>
            <el-button type="danger" text icon="Delete" @click="handleDelete(item)"
              v-hasPermi="['bk:friendship:remove']">删除</el-button>
          </div>
        </el-card>
      </el-col>

      <!-- Empty State -->
      <el-empty v-if="friendshipList.length === 0" description="暂无友链数据"></el-empty>
    </el-row>

    <!-- Pagination -->
    <div class="pagination-container">
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>

    <!-- Add/Edit Dialog -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="friendshipRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="网站名" prop="friendshipName">
          <el-input v-model="form.friendshipName" placeholder="请输入网站名" />
        </el-form-item>
        <el-form-item label="头像链接" prop="friendshipImg">
          <el-input v-model="form.friendshipImg" placeholder="请输入头像链接" />
          <div class="image-preview" v-if="form.friendshipImg">
            <el-image :src="form.friendshipImg" fit="cover" :preview-src-list="[form.friendshipImg]" />
          </div>
        </el-form-item>
        <el-form-item label="简介" prop="friendshipExcerpt">
          <el-input v-model="form.friendshipExcerpt" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="网站地址" prop="friendshipUrl">
          <el-input v-model="form.friendshipUrl" placeholder="请输入网站地址" />
        </el-form-item>
        <el-form-item label="邮箱" prop="friendshipEmail">
          <el-input v-model="form.friendshipEmail" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="审核状态" prop="friendshipStatus">
          <el-radio-group v-model="form.friendshipStatus">
            <el-radio v-for="dict in bk_friendship_status" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Friendship">
import { listFriendship, getFriendship, delFriendship, addFriendship, updateFriendship } from "@/api/bk/friendship";
import { User, Link, Message } from '@element-plus/icons-vue';

const { proxy } = getCurrentInstance();
const { bk_friendship_status } = proxy.useDict('bk_friendship_status');

const friendshipList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 12, // Increased for better card display
    friendshipId: null,
    friendshipName: null,
    friendshipImg: null,
    friendshipEmail: null,
    friendshipStatus: null
  },
  rules: {
    friendshipName: [
      { required: true, message: "网站名不能为空", trigger: "blur" }
    ],
    friendshipImg: [
      { required: true, message: "头像链接不能为空", trigger: "blur" }
    ],
    friendshipExcerpt: [
      { required: true, message: "简介不能为空", trigger: "blur" }
    ],
    friendshipUrl: [
      { required: true, message: "网站地址不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询友链信息列表 */
function getList() {
  loading.value = true;
  listFriendship(queryParams.value).then(response => {
    friendshipList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    friendshipId: null,
    friendshipName: null,
    friendshipImg: null,
    friendshipExcerpt: null,
    friendshipUrl: null,
    friendshipEmail: null,
    friendshipStatus: null
  };
  proxy.resetForm("friendshipRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加友链信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _friendshipId = row.friendshipId;
  getFriendship(_friendshipId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改友链信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["friendshipRef"].validate(valid => {
    if (valid) {
      if (form.value.friendshipId != null) {
        updateFriendship(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFriendship(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _friendshipId = row.friendshipId;
  proxy.$modal.confirm('是否确认删除友链 "' + row.friendshipName + '" 的数据？').then(function () {
    return delFriendship(_friendshipId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('bk/friendship/export', {
    ...queryParams.value
  }, `friendship_${new Date().getTime()}.xlsx`)
}

getList();
</script>

<style scoped>
.card-col {
  margin-bottom: 16px;
}

.friendship-card {
  transition: all 0.3s;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.friendship-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 12px 20px rgba(0, 0, 0, 0.1);
}

.card-header {
  background-color: #f5f7fa;
  padding: 20px;
  display: flex;
  justify-content: center;
  position: relative;
}

.card-header .avatar {
  border: 3px solid #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.status-tag {
  position: absolute;
  top: 10px;
  right: 10px;
}

.card-content {
  padding: 16px;
  flex-grow: 1;
}

.site-name {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 8px;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.site-excerpt {
  color: #606266;
  font-size: 14px;
  margin-bottom: 12px;
  height: 60px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

.site-info {
  border-top: 1px solid #ebeef5;
  padding-top: 12px;
}

.site-info p {
  margin: 8px 0;
  font-size: 13px;
  color: #606266;
  display: flex;
  align-items: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.site-info .el-icon {
  margin-right: 5px;
  font-size: 16px;
}

.card-footer {
  padding: 10px 16px;
  border-top: 1px solid #ebeef5;
  display: flex;
  justify-content: space-around;
}

.image-preview {
  margin-top: 8px;
  width: 100px;
  height: 100px;
  border-radius: 4px;
  overflow: hidden;
}

.image-preview .el-image {
  width: 100%;
  height: 100%;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

.el-empty {
  margin: 40px auto;
}
</style>